local miewei = fk.CreateSkill{
  name = "sclan__miewei",
}

Fk:loadTranslationTable{
  ["sclan__miewei"] = "灭威",
  [":sclan__miewei"] = "每轮限一次，你可以移去〖劲簇〗中的一项以无效一张伤害牌，然后摸等同于其序号数张牌。",

  ["#sclan__miewei-invoke"] = "灭威：是否移去〖劲簇〗中的一项以无效%dest使用的%arg",
  ["#sclan__miewei-remove"] = "移除：请移去〖劲簇〗中的一项",
}

---@param player ServerPlayer
local switchJincu = function (player)
  local mark = player:getMark("sclan__jincu")
  local current = mark.current
  while true do
    current = current + 1
    if current > 4 then current = current % 4 end
    if mark.all_names[current] ~= "" then
      break
    end
  end
  mark.current = current
  player.room:setPlayerMark(player, "sclan__jincu", mark)
end

miewei:addEffect(fk.CardUsing, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(miewei.name) and player:hasSkill("sclan__jincu") and
      data.card.is_damage_card and player:usedSkillTimes(miewei.name, Player.HistoryRound) == 0 and
        player:getMark("sclan__jincu").all_names and
          table.find(player:getMark("sclan__jincu").all_names, function (s)
            return s ~= ""
          end)
  end,
  on_cost = function (self, event, target, player, data)
    return player.room:askToSkillInvoke(player, {
      skill_name = miewei.name,
      prompt = "#sclan__miewei-invoke::"..target.id..":"..data.card:toLogString(),
    })
  end,
  on_use = function (self, event, target, player, data)
    local room = player.room
    local all_index = { "①", "②", "③", "④" }
    local all_des = { "射【杀】", "射【杀】", "射【杀】", "【一刀斩】" }
    local mark = player:getMark("sclan__jincu")
    local choices = {}
    local all_choices = { "①射【杀】", "②射【杀】", "③射【杀】", "④【一刀斩】" }
    for i = 1, 4 do
      if mark.all_names[i] ~= "" then
        table.insert(choices, all_index[i] .. all_des[i])
      end
    end
    local op = room:askToChoice(player, {
      choices = choices,
      all_choices = all_choices,
      cancelable = false,
      skill_name = miewei.name,
      prompt = "#sclan__miewei-remove"
    })

    mark.all_names[table.indexOf(all_choices, op)] = ""
    room:setPlayerMark(player, "sclan__jincu", mark)
    if mark.current == table.indexOf(all_choices, op) then
      switchJincu(player)
    end

    data.toCard = nil
    data:removeAllTargets()

    if player.dead then return end
    player:drawCards(table.indexOf(all_choices, op), miewei.name)
  end,
})

return miewei